cc.Class({
    extends: cc.Component,

    properties: {
        ribbons: cc.Prefab,
    },

    onLoad() {
        cc.Effect = this;
        this.shadow = this.node.find('shadow');
        this.shadow.parent = null;
        this.shadow.active = true;
        this.shadow.cascadeOpacity = false;
    },

    loadRibbons() {
        this.ribbonsAnim = cc.instantiate(this.ribbons).getComponent(cc.Animation);
        this.ribbonsAnim.node.parent = this.node;
        this.ribbonsAnim.node.x = 10000;
        this.ribbonsAnim.node.setLocalZOrder(100);
        this.ribbonsAnim.on('finished', () => this.ribbonsAnim.node.x = 10000, this);
    },

    shotScreen(node, cbk) {
        if(!this.ribbonsAnim) this.loadRibbons();
        var bg = cc.instantiate(this.shadow);
        bg.on('touchstart', event => event.stopPropagation(), this);
        bg.parent = this.node;
        bg.opacity = 0;
        var flash = cc.instantiate(this.shadow);
        var widget = flash.getComponent(cc.Widget);
        flash.color = cc.Color.WHITE;
        flash.parent = bg;
        flash.opacity = 0;
        widget.top = widget.bottom = widget.left = widget.right = -20;
        var shot = () => {
            this.ribbonsAnim.node.x = 0;
            this.ribbonsAnim.play();
            var rdr = cc.RenderTexture.create(GLOBAL.WIN_WIDTH, GLOBAL.WIN_HEIGHT);
            rdr.begin();
            node._sgNode.visit();
            rdr.end();
            bg.opacity = 156;
            flash.opacity = 255;
            flash._sgNode.addChild(rdr);
            flash.runAction(cc.rotateTo(0.4, 6).easing(cc.easeExponentialOut()));
            flash.runAction(cc.sequence(
                cc.scaleTo(0.4, 0.68).easing(cc.easeExponentialOut()),
                cc.delayTime(1),
                cc.callFunc(() => {
                    bg.parent = null;
                    cbk && cbk();
                }, this)
            ));
        }
        flash.runAction(cc.sequence(cc.fadeIn(0.3), cc.fadeOut(0.3), cc.callFunc(shot, this)));
    },
});
